package net.ibizsys.eclipse.studio.xml;

import net.ibizsys.eclipse.studio.tree.IIBizTreeData;
import net.ibizsys.eclipse.studio.util.StringKit;

import org.w3c.dom.Node;

/**
 * XML对象
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class XmlNode implements IIBizTreeData
{
	public static final String ATTR_ID = "ID";
	public static final String ATTR_NAME = "NAME";

	private String id = "";
	private String name = "";

	private IBizProject project;

	private IBizFileList fileList = new IBizFileList();

	public void init(Node node)
	{
		init(node, null);
	}

	public void init(Node node, IBizProject project)
	{
		this.project = project;

		for (int i = 0; i < node.getAttributes().getLength(); i++)
		{
			Node attr = node.getAttributes().item(i);
			if (attr.getNodeType() == Node.TEXT_NODE)
			{
				continue;
			}
			initAttribute(attr.getNodeName(), attr.getNodeValue());
		}

		for (int i = 0; i < node.getChildNodes().getLength(); i++)
		{
			if (node.getNodeType() == Node.TEXT_NODE)
			{
				continue;
			}
			initChildNode(node.getChildNodes().item(i), project);
		}
	}

	/**
	 * 初始化属性
	 * 
	 * @param name
	 * @param value
	 */
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_ID, name, true))
		{
			this.id = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_NAME, name, true))
		{
			this.name = value;
			return;
		}
	}

	/**
	 * 初始化子节点
	 * 
	 * @param node
	 */
	protected void initChildNode(Node node, IBizProject project)
	{
		if (StringKit.IsEqual(node.getNodeName(), IBizFileList.NODE_IBIZFILES, true))
		{
			fileList.init(node, project);
			return;
		}
	}

	public String getId()
	{
		return id;
	}

	public String getName()
	{
		return name;
	}

	public IBizFileList getFileList()
	{
		return fileList;
	}

	public IBizProject getProject()
	{
		return project;
	}

	@Override
	public String getText()
	{
		return getName();
	}

	@Override
	public String getIcon()
	{
		return "icons/package.png";
	}

	@Override
	public String getDesc()
	{
		return null;
	}

	public void sortChildren()
	{

	}
}
